package cn.iocoder.yudao.module.els.controller.admin.codeGeneratorSerial;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.els.controller.admin.codeGeneratorSerial.vo.*;
import cn.iocoder.yudao.module.els.dal.dataobject.codeGeneratorSerial.CodeGeneratorSerialDO;
import cn.iocoder.yudao.module.els.service.codeGeneratorSerial.CodeGeneratorSerialService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 编码生成器序列")
@RestController
@RequestMapping("/els/code-generator-serial")
@Validated
public class CodeGeneratorSerialController {

    @Resource
    private CodeGeneratorSerialService codeGeneratorSerialService;

    @PostMapping("/create")
    @Operation(summary = "创建编码生成器序列")
    @PreAuthorize("@ss.hasPermission('els:code-generator-serial:create')")
    public CommonResult<Long> createCodeGeneratorSerial(@Valid @RequestBody CodeGeneratorSerialSaveReqVO createReqVO) {
        return success(codeGeneratorSerialService.createCodeGeneratorSerial(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新编码生成器序列")
    @PreAuthorize("@ss.hasPermission('els:code-generator-serial:update')")
    public CommonResult<Boolean> updateCodeGeneratorSerial(@Valid @RequestBody CodeGeneratorSerialSaveReqVO updateReqVO) {
        codeGeneratorSerialService.updateCodeGeneratorSerial(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除编码生成器序列")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('els:code-generator-serial:delete')")
    public CommonResult<Boolean> deleteCodeGeneratorSerial(@RequestParam("id") Long id) {
        codeGeneratorSerialService.deleteCodeGeneratorSerial(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得编码生成器序列")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('els:code-generator-serial:query')")
    public CommonResult<CodeGeneratorSerialRespVO> getCodeGeneratorSerial(@RequestParam("id") Long id) {
        CodeGeneratorSerialDO codeGeneratorSerial = codeGeneratorSerialService.getCodeGeneratorSerial(id);
        return success(BeanUtils.toBean(codeGeneratorSerial, CodeGeneratorSerialRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得编码生成器序列分页")
    @PreAuthorize("@ss.hasPermission('els:code-generator-serial:query')")
    public CommonResult<PageResult<CodeGeneratorSerialRespVO>> getCodeGeneratorSerialPage(@Valid CodeGeneratorSerialPageReqVO pageReqVO) {
        PageResult<CodeGeneratorSerialDO> pageResult = codeGeneratorSerialService.getCodeGeneratorSerialPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CodeGeneratorSerialRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出编码生成器序列 Excel")
    @PreAuthorize("@ss.hasPermission('els:code-generator-serial:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCodeGeneratorSerialExcel(@Valid CodeGeneratorSerialPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CodeGeneratorSerialDO> list = codeGeneratorSerialService.getCodeGeneratorSerialPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "编码生成器序列.xls", "数据", CodeGeneratorSerialRespVO.class,
                        BeanUtils.toBean(list, CodeGeneratorSerialRespVO.class));
    }

}